package com.android.canvasproject.view.fragments;

import java.util.ArrayList;
import java.util.List;

import android.app.Fragment;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.android.canvasproject.R;
import com.android.canvasproject.adapter.MenuListAdapter;
import com.android.canvasproject.model.entity.MenuListItem;

public class HomeFragment extends Fragment {
	
	//CONSTANTES DE MENU
	private static final int MENU_MY_CANVAS = 0;
	private static final int MENU_CANVAS_CONTRIBUTOR = 1;
	private static final int MENU_PROFILE = 2;
	private static final int MENU_FRIENDS = 3;

	private ListView menuList;
	private String[] menuItemsTitles;
	private TypedArray menuItemsIcons;
	private List<MenuListItem> menuItems;
	private MenuListAdapter menuAdapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_home, container, false);
		
		menuItemsTitles = getResources().getStringArray(R.array.menu_list_items);
		menuItemsIcons = getResources().obtainTypedArray(R.array.menu_list_icons);

		menuList = (ListView) view.findViewById(R.id.listview_menu);
		menuItems = new ArrayList<MenuListItem>();

		for(int i = 0; i < menuItemsTitles.length; i++){
			menuItems.add(new MenuListItem(menuItemsTitles[i], menuItemsIcons.getResourceId(i, -1))); //MEUS CANVAS
//			menuItems.add(new MenuListItem(menuItemsTitles[1], menuItemsIcons.getResourceId(2, -1), true, "000")); //CANVAS PARTICIPANTES
//			menuItems.add(new MenuListItem(menuItemsTitles[2], menuItemsIcons.getResourceId(3, -1))); //PERFIL
//			menuItems.add(new MenuListItem(menuItemsTitles[3], menuItemsIcons.getResourceId(4, -1))); //AMIGOS
		}

		menuItemsIcons.recycle();

		menuAdapter = new MenuListAdapter(view.getContext(), menuItems);
		menuList.setAdapter(menuAdapter);
		menuList.setOnItemClickListener(new MenuClickListener());
		displayViewMenu(view, MENU_MY_CANVAS, 0L);

		return view;
	}
	
	private class MenuClickListener implements ListView.OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			displayViewMenu(view, position, id);
		}
	}
	
	private void displayViewMenu(View view, int position, long id){
		Fragment fragment = null;

		switch (position) {
			case MENU_MY_CANVAS:
				fragment = new MyCanvasFragment();
				break;

			case MENU_CANVAS_CONTRIBUTOR:
				fragment = new CanvasContributorFragment();
				break;

			case MENU_PROFILE:
				Toast.makeText(view.getContext(), "Tela não implementada", Toast.LENGTH_LONG).show();
				break;

			case MENU_FRIENDS:
				Toast.makeText(view.getContext(), "Tela não implementada", Toast.LENGTH_LONG).show();
				break;

			default:
				break;
		}
		
		if(fragment != null){
			getFragmentManager().beginTransaction().replace(R.id.frame_container_home, fragment).commit();
			menuList.setItemChecked(position, true);
			menuList.setSelection(position);
		} else
			Log.e("HOME_FRAGMENT", "Error in creating fragment");
	}
}